package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.mapper.SecKillGoodMapper;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import cn.wolfcode.shop.cloud.service.ISecKillGoodsService;
import org.apache.rocketmq.client.consumer.MessageQueueListener;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_FAIL_GROUP,
        topic = MQConstans.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstans.CREATE_ORDER_FAIL_TAG
)

@Component
public class RunOrderErrorMQListener implements RocketMQListener<Map> {



    @Autowired
    private ISecKillGoodsService secKillGoodsService;




    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(Map message) {

        Object seckillId = message.get("seckillId");
        String orderNo = (String) message.get("data");
        Long goodId = null;
        if (seckillId instanceof Long) {
            goodId = (Long) seckillId;
        } else if (seckillId instanceof Integer) {
            goodId = ((Integer) seckillId).longValue();
        }

        //回滚yukucun
        secKillGoodsService.syncPreCount(goodId);

        //本地标识拦截
        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_BROAD_DEST,goodId);
    }
}
